package edu.miami.group.photolog;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

//=============================================================================
public class LoginDatabase {

	// -----------------------------------------------------------------------------

	public static final String DATABASE_NAME = "login.db";
	private static final int DATABASE_VERSION = 1;
	private static final String TABLE_NAME = "LoginTable";
	private static final String CREATE_TABLE = "CREATE TABLE IF NOT EXISTS "
			+ TABLE_NAME
			+ "(_id INTEGER PRIMARY KEY AUTOINCREMENT, username TEXT, password TEXT);";

	private DatabaseHelper dbHelper;
	private SQLiteDatabase theDB;

	// -----------------------------------------------------------------------------
	public LoginDatabase(Context theContext) {

		dbHelper = new DatabaseHelper(theContext);
		theDB = dbHelper.getWritableDatabase();
	}

	// -----------------------------------------------------------------------------
	// Method that goes into the database and will retrieve the contents of a
	// cursor and put it into a contents value object
	public ContentValues cursorData(Cursor location) {
		int index = 0;
		if (location != null) {

			ContentValues contentObject = new ContentValues();
			String[] fieldNames = location.getColumnNames();

			for (index = 0; index < fieldNames.length; index++) {

				if (fieldNames[index].equals("_id")) {
					contentObject.put("_id",
							Integer.valueOf(location.getInt(index)));
				} else if (fieldNames[index].equals("username")) {
					contentObject.put("username", location.getString(index));
				} else if (fieldNames[index].equals("password")) {
					contentObject.put("password", location.getString(index));
				} 

			}
			return contentObject;
		}
		return null;
	}

	// ----------------------------------------------------------------------------------------------------
	// Adds a image to the database
	public boolean addUser(ContentValues userData) {

		return (theDB.insert(TABLE_NAME, null, userData) >= 0);

	}

	// --------------------------------------------------------------------------------------------------
	// closes the database
	public void close() {

		dbHelper.close();
		theDB.close();
	}

	// -----------------------------------------------------------------------------
	// fetches images in the database
	public Cursor fetchAllPosts() {

		String[] fieldNames = { "_id", "username", "password" };
		// changed the ordering to _id
		return (theDB.query(TABLE_NAME, fieldNames, null, null, null, null,
				"_id"));
	}

	// -------------------------------------------------------------------------------

	public ContentValues getDataByUsername(String username) {

		Cursor cursor = theDB.query(TABLE_NAME, null, "username = \'" + username + "\'",
				null, null, null, null);
		
		if(cursor.getCount() == 0) {
			return null;
		}
		cursor.moveToFirst();
		ContentValues returnObject = cursorData(cursor);
		cursor.close();
		return returnObject;

	}

	// =============================================================================
	private static class DatabaseHelper extends SQLiteOpenHelper {

		// -------------------------------------------------------------------------
		public DatabaseHelper(Context context) {

			super(context, DATABASE_NAME, null, DATABASE_VERSION);

		}

		// -------------------------------------------------------------------------
		public void onCreate(SQLiteDatabase db) {

			db.execSQL(CREATE_TABLE);
		}

		// -------------------------------------------------------------------------
		@Override
		public void onOpen(SQLiteDatabase db) {
			super.onOpen(db);

		}

		// -------------------------------------------------------------------------
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

			db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
			onCreate(db);
		}
		// -------------------------------------------------------------------------
	}
	// =============================================================================
}
// =============================================================================
